﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using DQS.BLL;
using DQS.EL;

public partial class Operate_Columns : DQS_Page
{
    private enum TABLEID 
    {
        USERINFO = 1001,
        PAYFEE = 1002,
        THISBILL = 1003,
        LASTBILL = 1004,
        BEFORELAST = 1005,
        TOTALBILL = 1006,
        RENTINFO = 1009
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetQueryID();
            Data_Init();
        }        
    }

    private void Data_Init()
    {
        UserColumns_Init();
        Check_DataBind(PayFeeColumns, (int)TABLEID.PAYFEE);
        Check_DataBind(ThisBillColumns, (int)TABLEID.THISBILL);
        Check_DataBind(LastBillColumns, (int)TABLEID.LASTBILL);
        Check_DataBind(BeforeLastColumns, (int)TABLEID.BEFORELAST);
        Check_DataBind(TotalBillColumns, (int)TABLEID.TOTALBILL);
        Check_DataBind(RentInfo, (int)TABLEID.RENTINFO);
    }

    private void UserColumns_Init()
    {
        Check_DataBind(UserColumns, (int)TABLEID.USERINFO);
        this.UserColumns.Items.FindByText("服务号码").Selected = true;
        this.UserColumns.Items.FindByText("服务号码").Enabled = false;
    }

    private void Check_DataBind(CheckBoxList argCheck, int argTableId)
    {
        argCheck.DataSource = DQ_COLUMNSManager.GetColumnsByTableID(argTableId);
        argCheck.DataTextField = "UI_COL";
        argCheck.DataValueField = "COL_ID";
        argCheck.DataBind();
    }

    private void SetQueryID()
    {
        if (Request.QueryString["QueryID"] == null)
        {
            DQ_USERS user = Session["LoginUser"] as DQ_USERS;
            Response.Redirect("~/Operate/Columns.aspx?queryid=" + (DQ_QUERYManager.NewQueryID(Convert.ToInt32(user.USER_ID)).ToString()));            
        }
    }

    protected void SaveColumn_Click(object sender, EventArgs e)
    {        
        IList<DQ_SETCOLUMNS> cols = new List<DQ_SETCOLUMNS>();
        GetSelectedValue(cols, UserColumns);
        GetSelectedValue(cols, PayFeeColumns);
        GetSelectedValue(cols, ThisBillColumns);
        GetSelectedValue(cols, LastBillColumns);
        GetSelectedValue(cols, BeforeLastColumns);
        GetSelectedValue(cols, TotalBillColumns);
        GetSelectedValue(cols, RentInfo);
        DQ_SetColumnsManager.InsertColumns(cols);
        Response.Redirect("~/Operate/Conditions.aspx?queryid=" + Request.QueryString["queryid"]);
    }

    private void GetSelectedValue(IList<DQ_SETCOLUMNS> cols, CheckBoxList argCBL)
    {
        string queryid = Request.QueryString["queryid"];
        foreach (ListItem lt in argCBL.Items)
        {
            if (lt.Selected)
            {
                DQ_SETCOLUMNS col = new DQ_SETCOLUMNS();
                col.QUERY_ID = Int32.Parse(queryid);
                col.COL_ID = Decimal.Parse(lt.Value);
                cols.Add(col);
            }
        }
    }
}
